Geospatial data sicence is a subset of data science that focuses on spactial data and its unique techniques. In this, we are going to perfomr spatial analysis and trying to find insights from spatial data. In this project, we lay the foundation for a career in Geospatial Data Science. You will get hands-on Geopy, plotly, etc the workhorse of geospatial data science Python libraries.
The topics covered in this project widely touch on some of the most used spatial technique in geospatial data science. We will be learning how read spatial data, manipulate and process spatial data using Pandas, and perform some spatial operations.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
!pip install plotly
import plotly.express as px
dir(px)
import plotly
import plotly.graph_objs as go
from plotly import tools
from plotly.offline import init_notebook_mode, plot, iplot
dir(plotly)
print(plotly.__version__)
current_data=pd.read_csv('https://raw.githubusercontent.com/datasets/covid-19/master/data/countries-aggregated.csv')
current_data.head(120)
current_data.tail(10)
Show interval dataa as colours. They are shaded in using one colour, where the darker shades represent high numbers and the lighter shades represent low numbers.
A choropleth map needs a key to explain what the different shapes mean.
fig=px.choropleth(current_data, locations='Country', locationmode='country names', color='Confirmed', animation_frame='Date')
fig.update_layout(title=('Choropleth Map of confirmed cases Till today'), template='plotly_dark')
fig.show()
fig=px.choropleth(current_data, locations='Country', locationmode='country names', color='Confirmed', animation_frame='Date', scope='europe')
fig.update_layout(title='Choropleth Map of confirmed cases Till today', template='plotly_dark')
fig.show()
fig=px.choropleth(current_data, locations='Country', locationmode='country names', color='Confirmed', animation_frame='Date', scope='asia')
fig.update_layout(title='Choropleth Map of confirmed cases Till today', template='plotly_dark')
fig.show()
fig=px.choropleth(current_data, locations='Country', locationmode='country names', color='Confirmed', animation_frame='Date', scope='north america')
fig.update_layout(title='Choropleth Map of confirmed cases Till today', template='plotly_dark')
fig.show()
fig=px.choropleth(current_data, locations='Country', locationmode='country names', color='Confirmed', animation_frame='Date', scope='south america')
fig.update_layout(title='Choropleth Map of confirmed cases Till today', template='plotly_dark')
fig.show()
fig=px.scatter_geo(current_data, locations='Country', locationmode='country names', color='Confirmed', size='Confirmed', hover_name='Country', animation_frame='Date', title='Spread over time')
fig.update(layout_template='plotly_dark')
fig.show()
current_data.columns
fig=px.choropleth(current_data, locations='Country', locationmode='country names', color='Recovered', animation_frame='Date')
fig.update_layout(title='Choropleth Map of Recovered cases Till today', template='plotly_dark')
fig.show()
fig=px.choropleth(current_data, locations='Country', locationmode='country names', color='Recovered', animation_frame='Date', scope='europe')
fig.update_layout(title='Choropleth Map of Recovered cases Till today', template='plotly_dark')
fig.show()
fig=px.choropleth(current_data, locations='Country', locationmode='country names', color='Recovered', animation_frame='Date', scope='north america')
fig.update_layout(title='Choropleth Map of Recovered cases Till today', template='plotly_dark')
fig.show()
fig=px.scatter_geo(current_data, locations='Country', locationmode='country names', color='Recovered', size='Recovered', hover_name='Country', animation_frame='Date', title='Recovery over time')
fig.update(layout_template='plotly_dark')
fig.show()
current_data.columns
fig=px.choropleth(current_data, locations='Country', locationmode='country names', color='Deaths', animation_frame='Date')
fig.update_layout(title='Choropleth Map of Deaths cases Till today', template='plotly_dark')
fig.show()
fig=px.scatter_geo(current_data, locations='Country', locationmode='country names', color='Deaths', size='Deaths', hover_name='Country', animation_frame='Date', title='Deaths over time')
fig.update(layout_template='plotly_dark')
fig.show()
fig=px.choropleth(current_data, locations='Country', locationmode='country names', color='Deaths', animation_frame='Date', scope='europe')
fig.update_layout(title='Choropleth Map of Recovered cases Till today', template='plotly_dark')
fig.show()
fig=px.choropleth(current_data, locations='Country', locationmode='country names', color='Deaths', animation_frame='Date', scope='north america')
fig.update_layout(title='Choropleth Map of Recovered cases Till today', template='plotly_dark')
fig.show()
fig=px.choropleth(current_data, locations='Country', locationmode='country names', color='Deaths', animation_frame='Date', scope='asia')
fig.update_layout(title='Choropleth Map of Recovered cases Till today', template='plotly_dark')
fig.show()
fig=px.choropleth(current_data, locations='Country', locationmode='country names', color='Deaths', animation_frame='Date', scope='south america')
fig.update_layout(title='Choropleth Map of Recovered cases Till today', template='plotly_dark')
fig.show()
!pip install geopy
import geopy
from geopy.geocoders import Nominatim
geolocator=Nominatim(user_agent='app')
location=geolocator.geocode('Eiffel Tower')
print(location.latitude, location.longitude)
df=current_data.copy()
df.head()
df[df['Country']=='Afghanistan']
df2=df.groupby(['Country'])[['Confirmed','Recovered','Deaths']].max().reset_index()
df2.head(10)
lat_lon=[]
geolocator=Nominatim(user_agent='app')
for location in df2['Country']:
location=geolocator.geocode(location)
if location is None:
lat_lon.append(np.nan)
else:
geo=(location.latitude, location.longitude)
lat_lon.append(geo)
lat_lon
df2['geo_loc']=lat_lon
df2.head()
type(lat_lon)
type(df2['geo_loc'][0])
lat,lon=zip(*np.array(df2['geo_loc']))
type(lat)
df2['lat']=lat
df2['lon']=lon
df2.head()
df2.drop('geo_loc', axis=1, inplace=True)
df2.head()
A tileset is a collection of raster or vector data broken up into a unifrom grid of square tiles.
A marker identifies a location on a map.
!pip install folium
import folium
folium.Map(location=[54,15], zoom_start=2)
m = folium.Map(location=[54,15], tiles='openstreetmap', zoom_start=2)
for id,row in df2.iterrows():
folium.Marker(location=[row['lat'],row['lon']], popup=row['Confirmed']).add_to(m)
m
m = folium.Map(location=[54,15], tiles='openstreetmap', zoom_start=2)
for id,row in df2.iterrows():
folium.Marker(location=[row['lat'],row['lon']], popup=row['Recovered']).add_to(m)
m
m = folium.Map(location=[54,15], tiles='openstreetmap', zoom_start=2)
for id,row in df2.iterrows():
folium.Marker(location=[row['lat'],row['lon']], popup=row['Deaths']).add_to(m)
m
Marker clusters can be a good way to simply a map containing many markers.
When the map is zoomed out nearly markers are combined together into a cluster, which is separated out when the map zoom level is closer.
from folium.plugins import MarkerCluster
mc=MarkerCluster()
m = folium.Map(location=[54,15], tiles='openstreetmap', zoom_start=2)
for id,row in df2.iterrows():
mc.add_child(folium.Marker(location=[row['lat'],row['lon']], popup=row['Confirmed']))
m.add_child(mc)
m
m = folium.Map(location=[54,15], tiles='openstreetmap', zoom_start=2)
for id,row in df2.iterrows():
mc.add_child(folium.Marker(location=[row['lat'],row['lon']], popup=row['Recovered']))
m.add_child(mc)
m
m = folium.Map(location=[54,15], tiles='openstreetmap', zoom_start=2)
for id,row in df2.iterrows():
mc.add_child(folium.Marker(location=[row['lat'],row['lon']], popup=row['Deaths']))
m.add_child(mc)
m
Geographic heat maps are an interactive way to identify where something occurs , and demostrate areas of high and low density.
Below is an exaample of a heat map showing the locations of Paul's Jr Restaurants. Read areas show where there are a high volume of Paul's Jr Restaurants in close proximity, while cooler areas show where there are fewer Paul's Jr Restaurants.
from folium.plugins import HeatMap
df2.head()
m=folium.Map(location=[54,15], tiles='openstreetmap', zoom_start=2)
HeatMap(data=df2[['lat','lon','Confirmed']],radius=15).add_to(m)
m
m=folium.Map(location=[54,15], tiles='openstreetmap', zoom_start=2)
HeatMap(data=df2[['lat','lon','Recovered']],radius=15).add_to(m)
m
m=folium.Map(location=[54,15], tiles='openstreetmap', zoom_start=2)
HeatMap(data=df2[['lat','lon','Deaths']],radius=15).add_to(m)
m